# -*- coding:utf8 -*-
# __author__ = "zhangying"
# __date__ = "2019/3/1 14:23"
"""
关键字模型
封装需要操作excel里面的所有方法
"""
from util.get_by_local import GETBYLOCAl
from base.base_driver import BaseDriver
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class ActionMethod:
    def __init__(self):
        '''
        一：调用安卓driver BaseDriver()
        二：调用定位信息 GETBYLOCAl().get_element
        三：调用get_element 里边的element_key 和value
        '''
        base_driver = BaseDriver()
        self.driver = base_driver.android_driver(0)
        self.get_by_local = GETBYLOCAl(self.driver)

    def input(self,*args):
        '''
        输入值 重点：(使用*args) 数据类型是list 字典 element_key 是第0个 例：args[0] value 是第一个args[1]

        一： driver.find_element_by_id('cn.com.open.mooc:id/account_edit').send_keys('13524559846')
        二：get_element  获取的是：获取element文件中的定位信息，以id,class,分割，数值为：
        if by == 'id':
                return  self.driver.find_element_by_id(local_by)
            elif by == 'className':
                return self.driver.find_element_by_class(local_by)
            else:
                return self.driver.find_element_by_xpath(local_by)

        :param element_key: 值为：定位信息   例：element_key = username 去LocalElement.ini文件中找username 这样就可以相对应了，找到定位信息  username = id>cn.com.open.mooc:id/account_edit
        :param value: 内容 例:send_keys('13524559846')
        :return:
        '''
        #举例：element = driver.find_element_by_id('cn.com.open.mooc:id/account_edit')

        element = self.get_by_local.get_element(args[0])
        if element == None:
            return args[0], "元素没找到"
        element.send_keys(args[1])

    def on_click(self,*args):
        """
        点击
        :param element_key:
        :return:
        """
        element = self.get_by_local.get_element(args[0])
        if element == None:
            return args[0], "元素没找到"
        element.click()

    def sleep_time(self,*args):
        """
        等待时间
        :param value:
        :return:
        """
        time.sleep(int(args[0]))


    # 获取屏幕的宽高
    def get_size(self,*args):
        size = self.driver.get_window_size()
        width = size['width']
        height = size['height']
        return width, height


    # 向左边滑动
    def swipe_left(self,*args):
        x1 = self.get_size()[0] / 10 * 9
        y1 = self.get_size()[1] / 2
        x = self.get_size()[0] / 10
        self.driver.swipe(x1, y1, x, y1)


    # 向右边滑动
    def swipe_right(self,*args):
        x1 = self.get_size()[0] / 10
        y1 = self.get_size()[1] / 2
        x = self.get_size()[0] / 10 * 9
        self.driver.swipe(x1, y1, x, y1)


    # 向上边滑动
    def swipe_up(self,*args):
        x1 = self.get_size()[0] / 2
        y1 = self.get_size()[1] / 10 * 6
        y = self.get_size()[1] / 10 * 2
        self.driver.swipe(x1, y1, x1, y)


    # 向下边滑动
    def swipe_dowm(self,*args):
        x1 = self.get_size()[0] / 2
        y1 = self.get_size()[1] / 10
        y = self.get_size()[1] / 10 * 9
        self.driver.swipe(x1, y1, x1, y)


    #封装找元素的方法
    def get_element(self,*args):
        element = self.get_by_local.get_element(args[0])
        if element == None:
            return None
        return element

    def get_tost_element(self,*args):
        '''
        获取tostelement
        :return:
        '''
        time.sleep(2)
        tost_elemet = ("xpath", "//*[contains(@text,"+args[0]+")]")
        return WebDriverWait(self.driver, 10, 0.1).until(EC.presence_of_all_elements_located(tost_elemet))








